﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using SmugMugApiWrapper;

namespace FillMyMug
{
    /// <summary>
    /// Interaction logic for ConfigurationWindow.xaml
    /// </summary>
    public partial class ConfigurationWindow : Window
    {

        ConfigurationInformation _config ;

        public ConfigurationWindow(ConfigurationInformation config) {

            InitializeComponent();

            Debug.Assert(config != null);

            _config = config;
            userNameTextBox.Text = _config.userName;
            userPasswordPasswordBox.Password = _config.userPassword;

            if (!(String.IsNullOrEmpty(_config.userName)) && !(String.IsNullOrEmpty(_config.userPassword))) {

                try
                {
                    SmugMugApi api = new SmugMugApi(_config.userName, config.userPassword);
                    List<SmugMugAlbumInfo> albums = api.GetAlbums();
                    albumListBox.ItemsSource = albums;
                    albumListBox.DisplayMemberPath = "AlbumName";
                    if (!String.IsNullOrEmpty(_config.defaultAlbum))
                    {
                        albumListBox.SelectedItem = _config.defaultAlbum;
                    }
                    savePasswordCheckbox.IsChecked = _config.saveUserLoginInfo;


                }
                catch {
                    MessageBox.Show("unable to connect to smugmug, check your username and password and try again", "Try Again", MessageBoxButton.OK, MessageBoxImage.Error);
                }
                
            }
            
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            
            
        }

        private void okButton_Click(object sender, RoutedEventArgs e)
        {
            
            _config.userName = userNameTextBox.Text.Trim();
            if ((bool)this.savePasswordCheckbox.IsChecked)
            {
                _config.userPassword = userPasswordPasswordBox.Password;
            }
            else {
                _config.userPassword = "";
            }
            SmugMugAlbumInfo ai = (SmugMugAlbumInfo)albumListBox.SelectedItem;
            _config.defaultAlbum = ai.AlbumName;
            _config.saveUserLoginInfo = (bool) this.savePasswordCheckbox.IsChecked;

            this.Close();
        }

        private void userPasswordPasswordBox_PasswordChanged(object sender, RoutedEventArgs e)
        {
            if ((bool)savePasswordCheckbox.IsChecked) savePasswordCheckbox.IsChecked = false;
        }


        private void cancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void savePasswordCheckbox_Checked(object sender, RoutedEventArgs e)
        {

            if (! (bool) savePasswordCheckbox.IsChecked) return;

            if ((!String.IsNullOrEmpty(userPasswordPasswordBox.Password) && (userPasswordPasswordBox.Password != _config.userPassword)))
            {
                try
                {
                    SmugMugApi api = new SmugMugApi(userNameTextBox.Text, userPasswordPasswordBox.Password);
                    albumListBox.ItemsSource = api.GetAlbums();
                    albumListBox.DisplayMemberPath = "AlbumName";
                }
                catch (Exception ex)
                {
                    albumListBox.ItemsSource = null;
                    albumListBox.Items.Clear();
                    albumListBox.Items.Add(ex.Message.ToString());
                    savePasswordCheckbox.IsChecked = false;
                }

            }
        }
    }
}
